#ifndef ESPNOWRECV_H
#define ESPNOWRECV_H
#include "./Config.h"




// 定义接收数据的结构体
DataInfo receivedPacket;

// 接收数据的回调函数
void OnDataRecv(const esp_now_recv_info* info, const uint8_t* incomingData, int len) {
  // 复制接收到的数据到 receivedPacket 结构体
  memcpy(&receivedPacket, incomingData, len);

  //判断消息是否已经在队列里
  for (int i = 0; i < receivedDataListSize; i++) {
    if (strcmp(receivedDataList[i].datainfo.id, receivedPacket.id) == 0) {
      //已经接收过数据
      return;
    }
  }

  //入队
  for (int i = 0; i < receivedDataListSize; i++) {
    if (receivedDataList[i].status == 0 || receivedDataList[i].status == 3) {
      receivedDataList[i].datainfo = receivedPacket;
      receivedDataList[i].status = 2;  //已消费



      // 打印接收到的数据信息
      // Serial.print("Received from MAC: ");
      // for (int i = 0; i < 6; i++) {
      //   Serial.print(info->src_addr[i], HEX);
      //   if (i < 5) Serial.print(":");
      // }
      Serial.println();
      Serial.print("sendMac: ");
      Serial.println(receivedPacket.sendMac);
      Serial.print("data: ");
      Serial.println(receivedPacket.data);
      Serial.print("id: ");
      Serial.println(receivedPacket.id);

      digitalWrite(led1_pin, HIGH);
      delay(10);
      digitalWrite(led1_pin, LOW);
      delay(10);
      digitalWrite(led1_pin, HIGH);


      break;
    }
  }
}





//初始化发送
void InitEspNowRecv() {

  // 注册接收回调函数
  esp_now_register_recv_cb(OnDataRecv);
}

#endif